package com.admission.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.ServletUtils;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Academic;
import com.admission.model.ArrayInfo;
import com.admission.model.Attention;
import com.admission.model.Examinee;
import com.admission.model.Experience;
import com.admission.model.Family;
import com.admission.model.Resume;
import com.admission.service.ArrayInfoManager;
import com.admission.service.AttentionManager;
import com.admission.service.ExamineeManager;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class ExamineeAdminAction extends CrudActionSupport<Examinee> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1333565165338003495L;

	private ExamineeManager examineeManager;
	private AttentionManager attentionManager;
	private Examinee examinee;
	private Academic academic;
	private String id;
	private Page<Examinee> page = new Page<Examinee>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private List<Experience> experiences;
	private List<Resume> resumes;
	private List<Family> families;
	private List<Attention> contents;
	private String subject; // 标识是运动员专题还是不知名专题
	private String registerNumber;
	private String testTime;
	private String searchIdentity;
	private String searchTicket;
	private String status;
	private String ids;
	private ArrayInfoManager arrayInfoManager;
	private String endTime;// 报名截止显示时间

	public Examinee getModel() {
		return examinee;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (!StringUtils.isEmpty(searchIdentity)) {
			PropertyFilter p1 = new PropertyFilter("EQS_identity",
					searchIdentity);
			filters.add(p1);
		}
		if (!StringUtils.isEmpty(searchTicket)) {
			PropertyFilter p2 = new PropertyFilter("EQS_ticket", searchTicket);
			filters.add(p2);
		}
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		page = examineeManager.pageExaminee(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		experiences = examinee.getExperiences();
		resumes = examinee.getResumes();
		families = examinee.getFamilies();
		academic = examinee.getAcademic();
		return "input";
	}

	public String detail() throws Exception {
		examinee = examineeManager.getById(id);
		contents = attentionManager.list();
		return "detail";
	}

	public String print() throws Exception {
		examinee = examineeManager.getById(id);
		contents = attentionManager.list();
		endTime = arrayInfoManager.getByItName(ArrayInfo.SPORTENDDATE)
				.getItemValue();
		return "print";
	}

	@Override
	public String save() throws Exception {
		examinee = examineeManager.getById(id);
		examinee.setRegisterNumber(registerNumber);
		examinee.setTestTime(testTime);
		examineeManager.save(examinee);
		return "detail";
	}

	public String excelOut() throws Exception {
		List<Examinee> list = examineeManager.list();
		Examinee examinee = null;

		XSSFWorkbook xwb = new XSSFWorkbook();
		CreationHelper createHelper = xwb.getCreationHelper();

		// create a new sheet
		Sheet s = xwb.createSheet();
		// declare a row object reference
		// Row r = null;
		// declare a cell object reference
		// Cell c = null;
		// create 2 cell styles
		CellStyle cs = xwb.createCellStyle();
		CellStyle cs2 = xwb.createCellStyle();
		DataFormat df = xwb.createDataFormat();

		// create 2 fonts objects
		Font f = xwb.createFont();
		Font f2 = xwb.createFont();

		// Set font 1 to 12 point type, blue and bold
		f.setFontHeightInPoints((short) 12);
		f.setColor(IndexedColors.RED.getIndex());
		f.setBoldweight(Font.BOLDWEIGHT_BOLD);

		// Set font 2 to 10 point type, red and bold
		f2.setFontHeightInPoints((short) 10);
		f2.setColor(IndexedColors.RED.getIndex());
		f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

		// Set cell style and formatting
		cs.setFont(f);
		cs.setDataFormat(df.getFormat("#,##0.0"));

		// Set the other cell style and formatting
		cs2.setBorderBottom(cs2.BORDER_THIN);
		cs2.setDataFormat(df.getFormat("text"));
		cs2.setFont(f2);

		// Define a few rows
		Row r = s.createRow(0);
		Cell c00 = r.createCell(0);
		Cell c01 = r.createCell(1);
		Cell c02 = r.createCell(2);
		Cell c03 = r.createCell(3);
		Cell c04 = r.createCell(4);
		Cell c05 = r.createCell(5);
		Cell c06 = r.createCell(6);
		Cell c07 = r.createCell(7);
		Cell c08 = r.createCell(8);
		Cell c09 = r.createCell(9);
		Cell c10 = r.createCell(10);
		Cell c11 = r.createCell(11);
		Cell c12 = r.createCell(12);
		Cell c13 = r.createCell(13);
		Cell c14 = r.createCell(14);
		Cell c15 = r.createCell(15);
		Cell c16 = r.createCell(16);
		Cell c17 = r.createCell(17);
		
		c00.setCellValue("姓名");
		c01.setCellValue("性别");
		c02.setCellValue("运动等级");
		c03.setCellValue("运动项目");
		c04.setCellValue("考生特征");
		c05.setCellValue("身份证");
		c06.setCellValue("考生号");
		c07.setCellValue("出生日期");
		c08.setCellValue("科类");
		c09.setCellValue("政治面貌");
		c10.setCellValue("民族");
		c11.setCellValue("生源省份");
		c12.setCellValue("身高(cm)");
		c13.setCellValue("体重(kg)");
		c14.setCellValue("Email");
		c15.setCellValue("联系电话");
		c16.setCellValue("文化课考试（分数线）");
		c17.setCellValue("联系地址");
		
		for (int rownum = 0; rownum < list.size(); rownum++) {
			examinee = list.get(rownum);
			r = s.createRow(rownum + 1);
			/*
			 * for (int cellnum = 0; cellnum < 10; cellnum += 2) { Cell c =
			 * r.createCell(cellnum); Cell c2 = r.createCell(cellnum + 1);
			 * 
			 * c.setCellValue((double) rownum + (cellnum / 10));
			 * c2.setCellValue(createHelper.createRichTextString("Hello! " +
			 * cellnum)); }
			 */
			c00 = r.createCell(0);
			c01 = r.createCell(1);
			c02 = r.createCell(2);
			c03 = r.createCell(3);
			c04 = r.createCell(4);
			c05 = r.createCell(5);
			c06 = r.createCell(6);
			c07 = r.createCell(7);
			c08 = r.createCell(8);
			c09 = r.createCell(9);
			c10 = r.createCell(10);
			c11 = r.createCell(11);
			c12 = r.createCell(12);
			c13 = r.createCell(13);
			c14 = r.createCell(14);
			c15 = r.createCell(15);
			c16 = r.createCell(16);
			c17 = r.createCell(17);

			c00.setCellValue(examinee.getName());
			c01.setCellValue(examinee.getGender());
			c02.setCellValue(examinee.getLevel());
			c03.setCellValue(examinee.getItem());
			c04.setCellValue(examinee.getFeature());
			c05.setCellValue(examinee.getIdentity());
			c06.setCellValue(examinee.getTicket());
			c07.setCellValue(examinee.getBirthday());
			c08.setCellValue(examinee.getType());
			c09.setCellValue(examinee.getPolitics());
			c10.setCellValue(examinee.getNation());
			c11.setCellValue(examinee.getProvince());
			c12.setCellValue(examinee.getHeight());
			c13.setCellValue(examinee.getWeight());
			c14.setCellValue(examinee.getEmail());
			c15.setCellValue(examinee.getPhone());
			c16.setCellValue(examinee.getMark());
			c17.setCellValue(examinee.getAddress());

		}

		// 输出Excel文件.
		HttpServletResponse response = Struts2Utils.getResponse();
		response.setContentType(ServletUtils.EXCEL_TYPE);
		ServletUtils.setFileDownloadHeader(response, "高水平运动员信息列表.xlsx");

		xwb.write(response.getOutputStream());
		response.getOutputStream().flush();
		return this.list();
	}
	
	
	/**
	 * 改变状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changeConsult() throws Exception {
		examineeManager.changeConsult(status);
		if (status.equals("1"))
			Struts2Utils.renderText("true");
		else
			Struts2Utils.renderText("false");
		return null;
	}

	@Override
	public String delete() throws Exception {
		examineeManager.deleteById(id);
		return this.list();
	}
	
	public String deleteAll() throws Exception {
		try{
			examineeManager.deleteExamineeByIds(ids);
		  // this.addActionMessage("操作成功");
	   } catch (Exception e) {
		// TODO: handle exception
		   this.addActionMessage("操作失败，请重新操作");
	   }  
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			examinee = new Examinee();
		} else {
			examinee = examineeManager.getById(id);
		}
	}

	@Autowired
	public void setExamineeManager(ExamineeManager examineeManager) {
		this.examineeManager = examineeManager;
	}

	public Examinee getExaminee() {
		return examinee;
	}

	public void setExaminee(Examinee examinee) {
		this.examinee = examinee;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Page<Examinee> getPage() {
		return page;
	}

	public void setPage(Page<Examinee> page) {
		this.page = page;
	}

	public List<Experience> getExperiences() {
		return experiences;
	}

	public Academic getAcademic() {
		return academic;
	}

	public void setAcademic(Academic academic) {
		this.academic = academic;
	}

	public List<Resume> getResumes() {
		return resumes;
	}

	public void setResumes(List<Resume> resumes) {
		this.resumes = resumes;
	}

	public List<Family> getFamilies() {
		return families;
	}

	public void setFamilies(List<Family> families) {
		this.families = families;
	}

	public void setExperiences(List<Experience> experiences) {
		this.experiences = experiences;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setRegisterNumber(String registerNumber) {
		this.registerNumber = registerNumber;
	}

	public void setTestTime(String testTime) {
		this.testTime = testTime;
	}

	public void setSearchIdentity(String searchIdentity) {
		this.searchIdentity = searchIdentity;
	}

	public void setSearchTicket(String searchTicket) {
		this.searchTicket = searchTicket;
	}

	@Autowired
	public void setAttentionManager(AttentionManager attentionManager) {
		this.attentionManager = attentionManager;
	}

	public List<Attention> getContents() {
		return contents;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public ArrayInfoManager getArrayInfoManager() {
		return arrayInfoManager;
	}
	@Autowired
	public void setArrayInfoManager(ArrayInfoManager arrayInfoManager) {
		this.arrayInfoManager = arrayInfoManager;
	}

}
